/**
 * @file
 * @auth zhaojing
 * @date 2016/11/24
 */

import './addAlipay.scss';

import Vue from 'vue';
import help from '../../../grsc-base/component/Help.vue';
import Popover from '../../../grsc-base/component/Popover.vue';
import Validator from '../../../grsc-base/component/Validator/main';
import {REG_EMAIL, REG_PHONE} from '../../../grsc-base/common/constants';
import commonUtil from '../../../grsc-base/common/commonUtil';
import modal from '../../../grsc-base/component/Modal.vue';

import commonService from '../../common/commonService';
import format from '../../common/format';

import tpl from './addAlipay.html';

const DO_ADD = 'ADD';
const DO_MOD = 'MOD';

export default  Vue.extend({
    name: 'AddModifyAlipay',
    template: tpl,
    props: {
        type: {
            type: String,
            default: DO_ADD
        },
        info: {
            type: Object,
            default() {
                return {};
            }
        },
        onSuccess: {
            type: Function,
            default() {

            }
        }
    },
    components: {
        modal,
        help,
        Popover,
        Validator
    },
    created() {
        if (this.type === DO_MOD) {
            this.form.name = this.info.name;
            this.form.alipayNo = this.info.alipayNo;
        }
    },
    mounted() {

    },
    watch: {

    },
    computed: {

    },
    data() {

        let title = '';
        if (this.type === DO_ADD) {
            title = '添加支付寶';
        } else {
            title = '修改支付寶';
        }

        return {
            commonUtil: commonUtil,

            title: title,
            form: {
                name: '',
                alipayNo: '',
                payPwd: ''
            },
            rules: {
                name: {
                    required: true,
                    requiredTip: this.$t('common.comp.addAlipay.name.requireTip'),
                    maxlength: 20,
                    maxlengthTip: this.$t('common.comp.addAlipay.name.maxTip'),
                },
                alipayNo: {
                    tip: this.$t('common.comp.addAlipay.account.tip'),
                    required: true,
                    requiredTip: this.$t('common.comp.addAlipay.account.requireTip'),
                    pattern: [
                        REG_PHONE,
                        [/^\S+@\S+$/, REG_EMAIL]    // 先验证前, 再验证后
                    ],
                    patternTip: this.$t('common.comp.addAlipay.account.patternTip')
                },
                payPwd: {
                    required: true,
                    requiredTip: this.$t('common.comp.addAlipay.payPwd.requireTip')
                }
            },
            isDisabled: true,
            hasSubmitted: false,
            isRequesting: false
        }
    },
    methods: {

        submit(){
            if (this.rules.$checkAll() === false) {
                return;
            }

            let param = {
                name: this.form.name,
                alipayNo: this.form.alipayNo
            };
            let header = {
                'pay-password': this.form.payPwd
            };

            this.isRequesting = true;
            commonService.updateAlipay(param, header).done(rep => {
                if (rep.code === window.CODE.SUCCESS) {
                    this.hide();
                    window.success(this.$t('common.success'), () => {
                        // 回到
                        this.onSuccess(param);
                    });
                // } else if (rep.code === window.CODE.EXIT_ALIPAY.CODE) {
                //     this.rules.alipayAccount.$setError(window.CODE.EXIT_ALIPAY.CH);
                } else if (rep.data && rep.data.num) {
                    let num = rep.data.num;
                    if (3 - num > 0) {
                        this.rules.payPwd.$setError(this.$t('common.IncorrectAssetPassTip',{num: 3-num}));
                    } else {
                        this.rules.payPwd.$setError(window.CODE.PAY_PWD_LOCK[this.$i18n.locale]);
                    }
                } else if (rep.code === window.CODE.PAY_PWD_LOCK.CODE) {
                    this.rules.payPwd.$setError(window.CODE.PAY_PWD_LOCK[this.$i18n.locale]);
                } else {
                    window.error(rep.msg);
                }
            }).always(() => {
                this.isRequesting = false;
            })
        },

        hide() {
            this.$emit('hideDialog');
        }

    }

});
